using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.Domain.Security;

using Liberty.Domain.CaseManagement;
using Liberty.Domain.NoteManagement;
using Liberty.Domain.ServiceMemberManagement;
using Liberty.Persistence.CaseManagement;
using Liberty.Persistence.NoteManagement;
using Liberty.Persistence.Kernel;

using Microsoft.VisualStudio.TestTools.UnitTesting;
//using NUnit.Framework.SyntaxHelpers;

namespace Tests.PersistenceTests.NoteManagement
{
    [TestClass()]
    public class NoteBrokerTests
    {
        #region Constants

        private const string CONNECTION_STRING = "ConnectionString";

        private const int
            TEST_SERVICE_MEMBER_ID = 3,
            TEST_CASE_ID = 5;

        private const string
            TEST_NOTE_DESCRIPTION = "Test note.";

        private const string
            ERROR_SERVICE_MEMBER_NOT_FOUND = "Service member not found.",
            ERROR_NO_NOTES_FOUND = "No notes found.";

        #endregion

        #region Properties

        protected string ConnectionString
        {
            get
            {
                return ConfigurationSettings.AppSettings[CONNECTION_STRING];
            }
        }

        #endregion

        #region Setup and Teardown
        [TestInitialize()]
        public void SetupNoteBrokerTests()
        {
        }

        [TestCleanup()]
        public void TeardownNoteBrokerTests()
        {
        }
        #endregion

        #region Test Methods

        [TestMethod()]
        public void TestNotesForServiceMember()
        {
            PersonBroker personBroker = new PersonBroker( this.ConnectionString );
            NoteBroker noteBroker = new NoteBroker( this.ConnectionString );
            ServiceMember serviceMember = personBroker.ServiceMemberWith( TEST_SERVICE_MEMBER_ID );
            Assert.IsNotNull( serviceMember, ERROR_SERVICE_MEMBER_NOT_FOUND );
            List<Note> notes = noteBroker.NotesFor( serviceMember );
            Assert.IsTrue( notes.Count > 0, ERROR_NO_NOTES_FOUND );
            Assert.AreEqual( notes.Count, new List<Note>( serviceMember.Notes ).Count );
        }

        [TestMethod()]
        public void TestNotesForCase()
        {
            CaseBroker broker = new CaseBroker( this.ConnectionString );
            Case myCase = broker.CaseWith( TEST_CASE_ID );
            NoteBroker noteBroker = new NoteBroker( this.ConnectionString );
            List<Note> notes = noteBroker.NotesFor( myCase );
            Assert.IsTrue( notes.Count > 0, ERROR_NO_NOTES_FOUND );
        }

        [TestMethod()]
        public void TestSaveCaseNote()
        {
            CaseBroker broker = new CaseBroker( this.ConnectionString );
            Case myCase = broker.CaseWith( TEST_CASE_ID );

            try
            {
                NoteBroker noteBroker = new NoteBroker( this.NewTransaction );

                List<Note> notes = noteBroker.NotesFor( myCase );
                int noteCount = notes.Count;

                User anAuthor = new User();
                Note newNote = new Note( anAuthor, TEST_NOTE_DESCRIPTION );
                noteBroker.SaveNote( myCase, newNote );
                notes = noteBroker.NotesFor( myCase );
                Assert.AreEqual( noteCount + 1, notes.Count );
            }
            finally
            {
                this.Rollback();
            }
        }

        #endregion

        #region Support Methods

        private SqlConnection CurrentConnection
        {
            get
            {
                if( i_cxn == null )
                {
                    i_cxn = new SqlConnection( this.ConnectionString );
                    i_cxn.Open();
                }
                return i_cxn;
            }
        }

        private SqlTransaction NewTransaction
        {
            get
            {
                if( i_txn == null )
                {
                    i_txn = this.CurrentConnection.BeginTransaction();
                }
                return i_txn;
            }
        }

        private void Rollback()
        {
            if( i_txn != null )
            {
                i_txn.Rollback();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void Commit()
        {
            if( i_txn != null )
            {
                i_txn.Commit();
                i_txn.Dispose();
                i_txn = null;
            }
        }

        private void CloseConnection()
        {
            if( i_cxn != null )
            {
                this.Rollback();
                i_cxn.Close();
                i_cxn.Dispose();
                i_cxn = null;
            }
        }
        #endregion

        #region Data Elements
        private SqlConnection i_cxn;
        private SqlTransaction i_txn;
        #endregion
    }
}
